package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAdmOrderTableField;
import com.zb.entity.dto.OrderTableFieldDto;
import com.zb.entity.vo.OrderTableFieldListVo;
import com.zb.mapper.ZjAdmOrderTableFieldMapper;
import com.zb.search.SearchDto;
import com.zb.service.ZjAdmOrderTableFieldService;
import com.zb.utils.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台工单列表表头字段表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-24
 */
@Service
public class ZjAdmOrderTableFieldServiceImpl extends ServiceImpl<ZjAdmOrderTableFieldMapper, ZjAdmOrderTableField> implements ZjAdmOrderTableFieldService {
    @Resource
    private ZjAdmOrderTableFieldMapper admOrderTableFieldMapper;


    // 保存或修改工单表头字段
    @Override
    public void save(OrderTableFieldDto fieldDto) {
        Assert.isNull(admOrderTableFieldMapper.existByName(fieldDto.getId(), fieldDto.getName()), "表头字段名称已存在");
        Assert.isNull(admOrderTableFieldMapper.existByCode(fieldDto.getId(), fieldDto.getCode()), "表头字段代码已存在");
        ZjAdmOrderTableField tableField = new ZjAdmOrderTableField();
        BeanUtil.copyProperties(fieldDto, tableField);
        tableField.setDisabled(fieldDto.getStatus());
        if (null != tableField.getId()) {
            this.updateById(tableField);
            return;
        }
        tableField.setId(IdWorker.getId());
        this.save(tableField);
    }

    // 获取表头字段列表
    @Override
    public Map<String, Object> getList(SearchDto search) {
        PageUtil.setPageLimit(search);
        List<ZjAdmOrderTableField> tableFields = admOrderTableFieldMapper.getList(search);
        return PageUtil.pageList(search.getPage(), search.getLimit(), admOrderTableFieldMapper.getListCount(search), tableFields.stream().map(field -> {
            OrderTableFieldListVo vo = new OrderTableFieldListVo();
            BeanUtil.copyProperties(field, vo);
            vo.setStatus(field.getDisabled());
            vo.setCreateTime(field.getGmtCreate());
            vo.setUpdateTime(field.getGmtUpdate());
            return vo;
        }).toList());
    }

    // 根据表头ID获取详情
    @Override
    public OrderTableFieldDto getDetail(Long id) {
        ZjAdmOrderTableField tableField = this.getById(id);
        Assert.notNull(tableField, "表头字段数据不存在");
        OrderTableFieldDto fieldDto = new OrderTableFieldDto();
        BeanUtil.copyProperties(tableField, fieldDto);
        fieldDto.setStatus(tableField.getDisabled());
        return fieldDto;
    }

    // 批量修改列隐藏或显示
    @Override
    public void updateShowCloumBatchById(boolean showColumn, List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        admOrderTableFieldMapper.updateShowCloumBatchById(ids, showColumn);
    }

    // 批量修改列冻结或解冻
    @Override
    public void updateFreezeCloumBatchById(boolean freezeColumn, List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        admOrderTableFieldMapper.updateFreezeCloumBatchById(ids, freezeColumn);
    }

    // 批量启用或禁用表头字段
    @Override
    public void updateDisabledBatchById(boolean status, List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        admOrderTableFieldMapper.updateDisabledBatchById(ids, status);
    }
}
